package homework.workTest.sort;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * SortUtils
 *
 * @author byc
 * @date 2021-11-24
 */
public class SortUtils {

    private final Integer BLOCK = 10000;

    /**
     * @param sort 当前最大排序
     */
    public static Double addNode(String sort) {
        return Double.valueOf(sort) + 10000;
    }

    /**
     * 取得两个数字的模糊中间数，并尽可能的忽略精确值。比如0.99与1.2的中间数是1
     *
     * @param bigNumberStr          [description]
     * @param smallNumberStr        [description]
     * @return [type]               [description]
     */
    public static Double getMiddleBetweenNumbers(String bigNumberStr, String smallNumberStr) {
        Double bigNumber = null;
        Double smallNumber = null;
        if (bigNumberStr != null) {
            bigNumber = Double.valueOf(bigNumberStr);
        }
        if (smallNumberStr != null) {
            smallNumber = Double.valueOf(smallNumberStr);
        }
        if (bigNumber == null) {
            Double precision = Math.min(getPrecisionOfNumber(smallNumberStr), -1.0);
            return smallNumber + Math.pow(10, Math.abs(precision));
        } else if (smallNumber == null) {
            Double precision = Math.min(getPrecisionOfNumber(bigNumberStr), -1.0);
            return bigNumber - Math.pow(10, Math.abs(precision));
        } else if (bigNumber.equals(smallNumber)) {
            return bigNumber;
        } else if (bigNumber < smallNumber) {
            return null;
        } else {
            Double middle = smallNumber + ((bigNumber - smallNumber) / 2);
            Integer minArr[] = {getPrecisionOfNumber(bigNumberStr), getPrecisionOfNumber(smallNumberStr), -1};
            Integer precisionMin = Arrays.stream(minArr).min(Comparator.comparingInt(o -> o)).get();
            Integer maxArr[] = {getPrecisionOfNumber(bigNumberStr), getPrecisionOfNumber(smallNumberStr), precisionMin};
            Integer precisionMax = Arrays.stream(maxArr).max(Comparator.comparingInt(o -> o)).get();
            for (int i = precisionMin + 1; i <= precisionMax + 1; i++) {
                BigDecimal b = new BigDecimal(middle);
                Double tmp = b.setScale(i, BigDecimal.ROUND_HALF_UP).doubleValue();
                if (tmp > smallNumber && tmp < bigNumber) {
                    return tmp;
                }
            }
        }
        return null;
    }

    /**
     * 重置排序
     */
    public static void resetSort(List<Double> doubleList, Double smallNum) {

        for (int i = 0; i < doubleList.size(); i++) {
            if (doubleList.get(i + 1) - doubleList.get(i) <0.000000001) {

            }
        }
    }


    /**
     * 取得数字的精确位，正数表示n位小数，负数表示精确到个十百千万位（10的(n-1)次方）
     */
    public static Integer getPrecisionOfNumber(String numberStr) {
        Number number;
        if (isNumeric(numberStr)) {
            number = Integer.valueOf(numberStr);
            number = Math.abs((Integer) number);
        } else {
            number = Double.valueOf(numberStr);
            number = Math.abs((Double) number);
        }
        numberStr = number + "";
        Integer len = (numberStr).length();
        if (numberStr.contains(".")) {
            return len - numberStr.indexOf(".") - 1;
        } else {
            for (int i = 1; i <= len; i++) {
                if (Double.valueOf(numberStr.substring(len - i, len - i + 1)) > 0) {
                    return 0 - (i - 1);
                }
            }
        }
        return null;
    }

    public static boolean isNumeric(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}
